package debtbundler.domain.entity.impl;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import debtbundler.domain.bll.Utils;
import debtbundler.domain.entity.Payment;
import debtbundler.domain.entity.Person;
import debtbundler.domain.entity.Share;
import debtbundler.domain.entity.SharedCost;
import debtbundler.domain.entity.Transaction;

class SharedCostImpl extends AbstractMoneyPoolImpl implements SharedCost {

	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(SharedCostImpl.class.getName());
	
	protected static long itemIdSeq = 1;
	
	protected SharedCostImpl() {
		super();
	
		name = "Kulu " + itemIdSeq;
		itemIdSeq++;
		
		logger.log(Level.FINE, "Created shared cost {0}", this);
	}

	@Override
	public boolean addPayment(Payment payment) {
		return this.addTransaction(payment);		
	}
	
	@Override
	public boolean addShare(Share share) {
		return this.addTransaction(share);		
	}	

	@Override
	public Set<Payment> getPayments() {
		HashSet<Payment> payments = new HashSet<Payment>();
		for (Transaction tr : getInputTransactions()) {
			payments.add( (Payment)tr );
		}
		return Collections.unmodifiableSet(payments);
	}


	@Override
	public Set<Share> getShares() {
		HashSet<Share> shares = new HashSet<Share>();
		for (Transaction tr : getOutputTransactions()) {
			shares.add( (Share)tr );
		}
		return Collections.unmodifiableSet(shares);
	}


	@Override
	public double getValue() {
		return Utils.sum(getPayments());
	}

	@Override
	public double shareEqually(Set<Person> participants) {
		return Utils.balanceByEqualSharing(this, participants);
	}

}
